var prmoseHttp = require('../../utils/prmoseHttp.js');
Page({
  data: {
    statusBarHeight: '',
    titleBarHeight: '',
    navHeight: '',
    bgHeight: '',
    imgHead:'https://rhi0.xiyun.net/zhuandian/image/',
    payAmt:'',
    accountInfo:{},
    userInfo:{},
    userId:'',
  },
  onLoad() {
    my.getSystemInfo({
      success: (res) => {
        this.setData({
          'statusBarHeight': res.statusBarHeight,//状态栏高度 
          'titleBarHeight': res.titleBarHeight,//标题栏高度
          'navHeight': res.statusBarHeight + res.titleBarHeight, //导航栏高度
          'bgHeight': res.statusBarHeight + res.titleBarHeight + 120//背景图片高度
        })
      }
    });
    this.setData({
      userInfo:my.getStorageSync({key: 'userInfo'}).data,
      userId:my.getStorageSync({key: 'userInfo'}).data.id
    })
    this.getAccount()
  },
  //账户信息
  getAccount(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.account,{},1,'get').then(res=>{
      if(res.code == 200){
        that.setData({
          accountInfo:res.data
        })
      }
    })
  },
  inputNumber(e){
    let that=this
    that.setData({
      payAmt: e.detail.value,
    });
    let price = e.detail.value;
    price = price.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
    price = price.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    price = price.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    price = price.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
    if (price.indexOf(".") < 0 && price != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
       price = parseFloat(price);
    }
    that.setData({
      payAmt: price,
    });
  },
  showAlert(){
    let that = this;
    if(that.data.payAmt < 1000){
      my.showToast({
        type:'fail',
        content:'兑换金币不能少于1000'
      });
    }else{
      let num1 = parseInt(that.data.payAmt/100);
      let num2 = (num1/100).toFixed(2)
      my.confirm({
        title: '温馨提示',
        content: '确认用'+that.data.payAmt+'个金币兑换为'+num2+'元吗？提现后不可撤销',
        cancelButtonText: '我再想想',
        confirmButtonText: '马上提现',
        success: (result) => {
          if(result.confirm == true){
            that.getMoney();
          }
        },
      });
    }
  },
  getMoney(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.exchange,{
      amount:that.data.payAmt,
    },1,'post').then(res=>{
      if(res.code == 200){
        my.showToast({
          type:'success',
          content:res.message
        });
        that.setData({
          payAmt:'',
        })
        that.getAccount();
      }else{
        my.showToast({
          type:'fail',
          content:res.data
        });
      }
    })
  },
  getAll(){
    this.setData({
      payAmt:this.data.accountInfo.residualCoin
    })
  },
  onShareAppMessage () {
    return {
      title: '挣点', // 标题
      desc: '任务多多，天天领取现金红包', // 描述
      path: 'pages/index/index?userid='+this.data.userId, // 分享的小程序页面
      success(res){
        my.navigateTo({
          url: '/pages/shareSuccess/shareSuccess'
        }); 
      },
      fail(res){
        console.log('fail',res)
      },
    }
  },
});
